﻿/******************************************************************************
    myBoard is a port of MacOS X dashboard application for windows.
    Copyright (C) 2007,2008  by Yann ALET
    Email : yann.alet@gmail.com

    myBoard is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    myBoard is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
****************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media.Imaging;
using System.Xml;
using System.ComponentModel;

namespace Guide.XMLTV
{
    /// <summary>
    /// This class represent a channel structure
    /// </summary>

    public class Channel : INotifyPropertyChanged
    {
        // Fields
        private BitmapImage _icon;
        private string _iconUrl;
        private string _id;
        private string _name;
        private List<Program> _programs = new List<Program>();
        private List<string> _programsFilters;

        // Events
        public event PropertyChangedEventHandler PropertyChanged;

        // Methods
        public Channel(XmlNode channel)
        {
            this._id = channel.Attributes["id"].Value;
            this._programsFilters = new List<string>();
            if (channel.HasChildNodes)
            {
                foreach (XmlNode node in channel.ChildNodes)
                {
                    if (node.Name == "display-name")
                    {
                        this._name = node.InnerText;
                    }
                    if (node.Name == "icon")
                    {
                        this._iconUrl = node.Attributes["src"].Value;
                    }
                }
            }
        }

        public void AddCategoryFilter(string category)
        {
            if (!this._programsFilters.Contains(category))
            {
                this._programsFilters.Add(category);
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("ProgramsView"));
                }
            }
        }

        public void RemoveCategoryFilter(string category)
        {
            if (this._programsFilters.Contains(category))
            {
                this._programsFilters.Remove(category);
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("ProgramsView"));
                }
            }
        }

        // Properties
        public BitmapImage Icon
        {
            get
            {
                return this._icon;
            }
            set
            {
                this._icon = value;
            }
        }

        public string IconUrl
        {
            get
            {
                return this._iconUrl;
            }
            set
            {
                this._iconUrl = value;
            }
        }

        public string Id
        {
            get
            {
                return this._id;
            }
            set
            {
                this._id = value;
            }
        }

        public string Name
        {
            get
            {
                return this._name;
            }
            set
            {
                this._name = value;
            }
        }

        public List<Program> Programs
        {
            get
            {
                return this._programs;
            }
            set
            {
                this._programs = value;
            }
        }

        public List<Program> ProgramsView
        {
            get
            {
                List<Program> list = new List<Program>();
                foreach (Program program in this._programs)
                {
                    if (program.Categories != null)
                    {
                        foreach (string str in this._programsFilters)
                        {
                            if (program.Categories.Contains(str))
                            {
                                list.Add(program);
                            }
                        }
                    }
                }
                return list;
            }
        }

    }
}
